home *** CD-ROM | disk | FTP | other *** search
/ PC-Blue - MS DOS Public Domain Library / PC-Blue MS-DOS Public Domain Library - NYACC.iso / vol270 / contour2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-12-16  |  3.2 KB  |  126 lines

  1. /*    contour2 - subsidiary routines for contour drawing 
  2.  
  3.     None of these routines refers to a global variable 
  4.  
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <math.h>
  9.  
  10. #define maximum(a,b) ((a)>(b)?(a):(b))
  11.  
  12. int mygets(buf,size,f,fout) char *buf; int size; FILE f,fout;
  13. {    char *t;
  14.     while(1)
  15.         {if(fgets(buf,size,f)==0) {return 0;}
  16.         t=buf;
  17.         while(*t && isspace(*t)) t++;
  18.         if(*t == 0) continue;        /* skip blank lines */
  19.         buf[strlen(buf)-1]=0;        /* zero out the line feed */
  20.         if(buf[0]==';')
  21.             {printf("%s\n",buf); continue;
  22.             fprintf(fout,"%s\n",buf); continue;
  23.             }  /* skip comment */
  24.         return 1;
  25.         }
  26. }
  27.  
  28. get_double(argc,argv,permitted,a,b,c)
  29. int argc,permitted; char **argv; double *a,*b,*c;
  30. {    int i=1;
  31.     if((permitted--)>0 && (argc>i) && numeric(argv[i])) *a=atof(argv[i++]);
  32.     if((permitted--)>0 && (argc>i) && numeric(argv[i])) *b=atof(argv[i++]);
  33.     if((permitted--)>0 && (argc>i) && numeric(argv[i])) *c=atof(argv[i++]);
  34.     return i;
  35. }
  36.  
  37. int streq(a,b) char *a,*b;
  38. {    while(*a)
  39.         {if(*a!=*b)return 0;
  40.         a++; b++;
  41.         }
  42.     return 1;
  43. }
  44.  
  45. gripe_arg(s) char *s;
  46. {    fprintf(stderr,"argument missing for switch %s",s);
  47.     help();
  48. }
  49.  
  50. gripe(argv) char **argv;
  51. {    fprintf(stderr,*argv); fprintf(stderr," isn\'t a legal argument \n\n");
  52.     help();
  53. }
  54.  
  55. numeric(s) char *s;
  56. {    char c;
  57.     while(c=*s++)
  58.         {if((c<='9' && c>='0') || c=='+' || c=='-' || c=='.') continue;
  59.         return 0;
  60.         }
  61.     return 1;
  62. }
  63.  
  64.  
  65. static double decide[3]=    {1.414,    3.162,    7.071};
  66. static int mtic[4]=        {1,  2,  5, 10};
  67.  
  68. scale_one (amin,amax,bmin,bmax,lab_requested,nlab,kind)
  69.     double amin,amax,*bmin,*bmax; int lab_requested, *nlab, kind;
  70. {    double tens, top, bottom, fraction, d;
  71.     int i,j;
  72.  
  73.     if(!kind)    /* linear scale */
  74.         {if(amax<=amin) amax=amin+1.;
  75.         fraction=(amax-amin)/maximum(lab_requested-1,1);
  76. #ifdef xxx
  77.         printf("\nscale_one: fraction=%f",fraction);
  78. #endif
  79.         tens=pow(10.,floor(log10(fraction)));
  80.         fraction/=tens;
  81.         for (j=0; j<=2; j++) if(fraction<decide[j]) break;
  82.         d=mtic[j]*tens;                /* data increment between labels */
  83.         bottom=floor(amin/d);    *bmin=bottom*d;
  84.         top=ceil(amax/d);        *bmax=top*d;
  85.         *nlab=(int)(top-bottom+.25);    /* # labels */
  86. #ifdef xxx
  87.         printf("(-->%f) ?= d=%f, tens=%f \n",fraction,d,tens);
  88.         printf("       bottom=%f, top=%f, j=%d  \n",
  89.             bottom,top,j);
  90.         printf("labels range from  bmin=%f  to  bmax=%f  in  nlab=%d  steps \n", 
  91.             *bmin, *bmax, *nlab);
  92. #endif
  93.         }
  94.     else    /* log scale */
  95.         {*bmin=floor(amin+.001); *bmax=ceil(amax-.001);
  96.         *nlab= *bmax - *bmin + .1;
  97. #ifdef xxx
  98.     printf("\n scale_one: *bmin=%f  *bmax=%f  *nlab=%d   kind=%d \n",
  99.         *bmin, *bmax, *nlab, kind);
  100. #endif
  101.         }
  102. }
  103.  
  104. /*    return scale factor and format for printing nlx labels from x1 to x2 */
  105. double adjust(fmt,x1,x2,nlx) char *fmt; double x1,x2; int nlx;
  106. {    double large,a1,a2,adj=1.;
  107.     int exponent=0,after;    /* "after" is # digits needed after decimal */
  108.     a1=fabs(x1);
  109.     a2=fabs(x2);
  110.     large=(a1>a2)?a1:a2;
  111.     if(large<.01)
  112.         while(large*adj < 1.) {adj *= 1000.; exponent -= 3;}
  113.     while(large*adj > 1000.) {adj /= 1000.; exponent += 3;}
  114.     after=ceil(-log10((x2*adj-x1*adj)/nlx));
  115.     if(after<0) after=0;
  116.     if(exponent)
  117.         {sprintf(fmt," %%%d.%dfe%d",after,after,exponent);
  118.         }
  119.     else
  120.         {sprintf(fmt," %%%d.%df",after,after);
  121.         }
  122.     return (adj);
  123. }
  124.  
  125.  
  126.